package com.liqi.device.service.impl;

import com.liqi.common.annotation.DataScope;
import com.liqi.common.utils.DateUtils;
import com.liqi.common.utils.SecurityUtils;
import com.liqi.common.utils.excel.ExcelUtils;
import com.liqi.device.domain.DvDrive;
import com.liqi.device.mapper.DvDriveMapper;
import com.liqi.device.service.IDvDriveService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 驱动管理Service业务层处理
 *
 * @author Lwy
 * @date 2022-03-15
 */
@Service
public class DvDriveServiceImpl implements IDvDriveService
{
    @Autowired
    private DvDriveMapper dvDriveMapper;


    @Override
    public DvDrive selectDvDriveById(Long id) {
        return dvDriveMapper.selectDvDriveById(id);
    }

    @Override
    public DvDrive selectDvDriveByDriveCode(String driveCode) {
        return dvDriveMapper.selectDvDriveByDriveCode(driveCode);
    }

    @Override
    public DvDrive selectDvDriveByAdapterName(String adapterName) {
        return dvDriveMapper.selectDvDriveByAdapterName(adapterName);
    }

    /**
     * 查询驱动管理列表
     *
     * @param dvDrive 驱动管理
     * @return 驱动管理
     */
    @Override
    @DataScope(deptAlias = "d", userAlias = "u")
    public List<DvDrive> selectDvDriveList(DvDrive dvDrive)
    {
        return dvDriveMapper.selectDvDriveList(dvDrive);
    }

    /**
     * 新增驱动管理
     *
     * @param dvDrive 驱动管理
     * @return 结果
     */
    @Override
    public int insertDvDrive(DvDrive dvDrive)
    {
        dvDrive.setCreateTime(DateUtils.getNowDate());
        return dvDriveMapper.insertDvDrive(dvDrive);
    }

    /**
     * 修改驱动管理
     *
     * @param dvDrive 驱动管理
     * @return 结果
     */
    @Override
    public int updateDvDrive(DvDrive dvDrive)
    {
        dvDrive.setUpdateTime(DateUtils.getNowDate());
        return dvDriveMapper.updateDvDrive(dvDrive);
    }

    @Override
    public String importDvdrive(List<DvDrive>  dvDrives) {
        StringBuilder sb=new StringBuilder();

        for(DvDrive dvDrive:dvDrives){
            String driveName = dvDrive.getDriveName();
            String adapterName = dvDrive.getAdapterName();
            String driveCode = dvDrive.getDriveCode();
            boolean b = ExcelUtils.checkStringIsNull(driveName, driveCode, adapterName);
            if(b==false){
                sb.append("第" + dvDrive.getRowNum() + "行：有值未填");
                if (sb.length() > 0 && sb.charAt(sb.length() - 1) == ',') {
                    sb.deleteCharAt(sb.length() -1 );
                }
                sb.append(";<br/>");
            }else {
                DvDrive drive = dvDriveMapper.selectDvDriveByDriveCode(driveCode);
                if (drive == null) {
                    drive = new DvDrive();
                    drive.setDriveName(driveName);
                    drive.setDriveCode(driveCode);
                    drive.setAdapterName(adapterName);
                    drive.setCreateUserId(SecurityUtils.getUserId().toString());
                    drive.setDeptId(SecurityUtils.getDeptId().toString());
                    dvDriveMapper.insertDvDrive(drive);
                }
            }
        }

        return sb.toString();
    }

    /**
     * 批量删除驱动管理
     *
     * @param ids 需要删除的驱动管理主键
     * @return 结果
     */
    @Override
    public int deleteDvDriveByIds(Long[] ids)
    {
        return dvDriveMapper.deleteDvDriveByIds(ids);
    }

    /**
     * 删除驱动管理信息
     *
     * @param id 驱动管理主键
     * @return 结果
     */
    @Override
    public int deleteDvDriveById(Long id)
    {
        return dvDriveMapper.deleteDvDriveById(id);
    }

    @Override
    public List<DvDrive> dict() {
        return dvDriveMapper.dict();
    }


}
